//
//  HomeRepository.m
//  Project
//
//  Created by liting on 2025/8/29.
//

#import "HomeRepository.h"
#import "LZFileLoader.h"
#import "BannerModel.h"
#import "YYModel.h"
#import "ItemModel.h"
#import "RecommendModel.h"

@implementation HomeRepository

#pragma mark - 单例
+ (instancetype)sharedLoader {
    static HomeRepository *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}


#pragma mark - 请求轮播图
- (NSArray<BannerModel*>*)getBannerData{
    NSDictionary *data = [[LZFileLoader sharedLoader] loadJSONFromMainBundleWithFileName:@"home_recommend_banners" fileType:nil];
    if (!data) {
        NSLog(@"加载用户数据失败");
        return @[];
    }

    NSArray *arr = data[@"data"];
    NSMutableArray<BannerModel *> *dataArray =[NSMutableArray array];
    for (int i = 0; i<arr.count; i++) {
        BannerModel*model = [BannerModel yy_modelWithDictionary:arr[i]];
        [dataArray addObject:model];
    }
    return dataArray;
}

#pragma mark - 请求列表
- (NSArray<ItemModel*>*)getListDataWithPage:(int)page{
    NSMutableArray<ItemModel *> *dataArray = [NSMutableArray array];
    NSDictionary *data = [[LZFileLoader sharedLoader] loadJSONFromMainBundleWithFileName:[NSString stringWithFormat:@"home_recommend_list%d",page] fileType:nil];
    if (!data) {
        NSLog(@"加载用户数据失败");
        return @[];
    }

    NSArray *arr = data[@"data"][@"feeds"];
    for (int i = 0; i<arr.count; i++) {
        ItemModel*model = [ItemModel yy_modelWithDictionary:arr[i]];
        [dataArray addObject:model];
    }
    return dataArray;
}


#pragma mark - 请求推荐列表
- (NSArray<RecommendModel*>*)getRecommendListData{
    NSMutableArray<RecommendModel *> *dataArray = [NSMutableArray array];
    NSDictionary *data = [[LZFileLoader sharedLoader] loadJSONFromMainBundleWithFileName:@"novel_detail_recommend" fileType:nil];
    if (!data) {
        NSLog(@"加载用户数据失败");
        return @[];
    }

    NSArray *arr = data[@"data"][@"infos"];
    for (int i = 0; i<arr.count; i++) {
        RecommendModel*model = [RecommendModel yy_modelWithDictionary:arr[i]];
        [dataArray addObject:model];
    }
    return dataArray;
}


@end
